import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/person/userAccount'

const api = {

    /**
     * 新增员工账户
     * @param userAccount
     * @returns {Promise<*|undefined>}
     */
    createUserAccount: (userAccount) =>
      axiosClient().post(`${requestUrl}/addInfo`, userAccount),

    /**
     * 根据ID删除员工账户
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteUserAccountById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新员工账户
     * @param id
     * @param userAccount
     * @returns {Promise<*|undefined>}
     */
    updateUserAccountFull: (id, userAccount) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, userAccount),

    /**
     * 根据ID部分更新员工账户
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateUserAccountPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询员工账户
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getUserAccountById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询员工账户
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getUserAccountPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询员工账户
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getUserAccountList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.userBaseId) {
            params.append('userBaseId', businessParam.userBaseId)
        }
        if (businessParam.loginName) {
            params.append('loginName', businessParam.loginName)
        }
        if (businessParam.loginMobile) {
            params.append('loginMobile', businessParam.loginMobile)
        }
        if (businessParam.loginEmail) {
            params.append('loginEmail', businessParam.loginEmail)
        }
        if (businessParam.password) {
            params.append('password', businessParam.password)
        }
        if (businessParam.passwordExpireTime) {
            params.append('passwordExpireTime', businessParam.passwordExpireTime)
        }
        if (businessParam.accountStatus) {
            params.append('accountStatus', businessParam.accountStatus)
        }
        if (businessParam.lockReason) {
            params.append('lockReason', businessParam.lockReason)
        }
        if (businessParam.lastLoginTime) {
            params.append('lastLoginTime', businessParam.lastLoginTime)
        }
        if (businessParam.lastLoginIp) {
            params.append('lastLoginIp', businessParam.lastLoginIp)
        }
        if (businessParam.loginFailCount) {
            params.append('loginFailCount', businessParam.loginFailCount)
        }
        if (businessParam.loginFailTime) {
            params.append('loginFailTime', businessParam.loginFailTime)
        }
        if (businessParam.mfaStatus) {
            params.append('mfaStatus', businessParam.mfaStatus)
        }
        if (businessParam.mfaSecret) {
            params.append('mfaSecret', businessParam.mfaSecret)
        }
        if (businessParam.accountExpireTime) {
            params.append('accountExpireTime', businessParam.accountExpireTime)
        }
        if (businessParam.loginChannel) {
            params.append('loginChannel', businessParam.loginChannel)
        }
        if (businessParam.tenantId) {
            params.append('tenantId', businessParam.tenantId)
        }
        if (businessParam.applicationId) {
            params.append('applicationId', businessParam.applicationId)
        }
        if (businessParam.createUserId) {
            params.append('createUserId', businessParam.createUserId)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
